package ipad.asia.controller;

import ipad.asia.dao.ConsumerDao;
import ipad.asia.dao.PunishConsumerDao;
import ipad.asia.dao.ShoppingCartDao;
import ipad.asia.entity.*;
import ipad.asia.service.ConsumerService;
import ipad.asia.utils.MD5Util;
import ipad.asia.utils.MyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

@RestController
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ConsumerDao consumerDao;
    @Autowired
    private PunishConsumerDao punishConsumerDao;
    @Autowired
    private MD5Util md5Util;
    @Autowired
    private MyTime myTime;
    @Autowired
    private ShoppingCartDao shoppingCartDao;
//    @Autowired
//    private ShoppingCartService shoppingCartService;
    String salt=md5Util.RandomSalt();
    /**
     * 登录说明：
     * 3是表示封号，禁止登录，跳转到申请解锁页面
     * 2表示严重警告！需要处理被举报的东西
     * 1表示举报，警告你不要再犯错误
     * 4表示可以正常登录
     * 0表示账号密码错误
     * @param user
     * @param password
     * @return
     * @throws IOException
     */
    @RequestMapping("/consumer/login")
    public int login(@RequestParam(value = "user",required = false) String user,
                     @RequestParam(value = "password", required = false) String password) throws IOException {
        Consumer consumer = consumerService.findByUser(user);
        if (null != consumer) {
            String newPW = MD5Util.FormPassToDbPass(password,consumer.getSalt());
            if (newPW.equals(consumer.getPassword())) {
                List<PunishConsumer> punishConsumers = punishConsumerDao.findByConsumerId(consumer.getId());
                System.out.println(punishConsumers);
                for (int i = 0; i < punishConsumers.size(); i++) {
                    if (punishConsumers.get(i).getSerious() == 3) {
                        return 3;
                    }
                    if (punishConsumers.get(i).getSerious() == 2) {
                        return 2;
                    }
                    if (punishConsumers.get(i).getSerious() == 1) {
                        return 1;
                    }
                }
                return 4;
            }
        }
        return 0;
    }
    /**
     * 直接注册，不用多说
     * @param user
     * @param password
     * @param email
     * @param phone
     * @param
     * @return
     */
    @RequestMapping ("/consumer/register")
    public boolean register(@RequestParam(value = "user",required = false) String user,
                            @RequestParam(value = "password", required = false) String password,
                            @RequestParam(value = "email",required = false) String email,
                            @RequestParam(value = "phone",required = false) String phone
    ) {
        if (user == null) {
            return false;
        } else {
            System.out.println("phone-------:"+phone);
            System.out.println("user--------:"+consumerService.findByUser(user));
            if (null==consumerService.findByUser(user)) {
//                    && shopService.findByName(shopName).isEmpty()
                Consumer consumer = new Consumer();
                consumer.setUser(user);
//                String PW= md5Util.inputPassToFormPass(password);//这个是前端使用
                String md5PW=md5Util.FormPassToDbPass(password,salt);//这个是后端使用
                consumer.setSalt(salt);
                consumer.setUser(user).setEmail(email).setPhone(phone);
                consumer.setPassword(md5PW);
                consumer.setDate(myTime.LocalDateTime());
                consumerService.save(consumer);
                System.out.println("ok-----------------------------");
                return true;
            }else {return false;}
        }
    }
    /**
     * 查找id，用作session
     * @param id
     * @return
     */
    @RequestMapping("/consumer/get")
    public Consumer getId(@RequestParam(value = "id",required = false) int id){
            return consumerService.findById(id);
    }
    /**
     * 查找name，用作session
     * @param user
     * @return
     */
    @RequestMapping("/consumer/getName")
    public Consumer getName(@RequestParam(value = "user",required = false) String user){
        return consumerService.findByUser(user);
    }


    @RequestMapping("/consumer/findAll")
    public List<Consumer> findAll(){
        return consumerDao.findAll();
    }


    /**
     * 更新个人信息
     * @param user
     * @param phone
     * @param email
     * @param sex
     * @param signature
     */
    @Transactional
    @RequestMapping(value = "/consumer/updateInfo")
    public void updateInfo( @RequestParam(value = "user",required = false)String user,
                        @RequestParam(value = "phone",required = false) String phone,
                        @RequestParam(value = "email",required = false) String email,
                        @RequestParam(value = "sex",required = false) String sex,
                        @RequestParam(value = "address",required = false) String address,
                        @RequestParam(value = "signature",required = false) String signature){
         consumerDao.updateInfo(user,phone,email,sex,address,signature);
    }

    /**
     * 将商品加入购物车
     * @param consumerId
     * @param productId
     * @param shopId
     * @param count
     */
    @RequestMapping("/consumer/cart/add")
    public void CartAdd(@RequestParam(value = "consumerId",required = false)int consumerId,
                        @RequestParam(value = "productId",required = false)int productId,
                        @RequestParam(value = "shopId",required = false)int shopId,
                        @RequestParam(value = "count",required = false)int count){
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setConsumerId(consumerId).setProductId(productId).
                setShopId(shopId).setCount(count).setDate(myTime.LocalDateTime());
        shoppingCartDao.save(shoppingCart);
    }

    /**
     * 查看自己的购物车
     * @return
     */
    @RequestMapping("/consumer/cart")
    public List<ShoppingCart> getCart(@RequestParam(value = "consumerId",required = false) int consumerId) {
            return shoppingCartDao.findByConsumerId(consumerId);
    }

    /**
     * 删除购物车商品
     * @param productId
     * @param consumerId
     */
    @Transactional
    @RequestMapping("/consumer/cart/deleteByProductId")
    public void deleteByProductIdAndConsumerId(@RequestParam(value = "productId",required = false) int productId,
                                               @RequestParam(value = "consumerId",required = false) int consumerId){
        shoppingCartDao.deleteByProductIdAndConsumerId(productId, consumerId);
    }

    /**
     * 清空购物车
     */
    @RequestMapping("/consumer/cart/deleteAll")
    public void deleteAll(){
        shoppingCartDao.deleteAll();
    }

    @RequestMapping("/test/{passwd}")
    public  String  test(@PathVariable(value = "passwd")String passwd){
      String result= md5Util.inputPassToDbPass(passwd,salt);
        String PW= md5Util.inputPassToFormPass(passwd);//这个是前端使用
        String md5PW=md5Util.FormPassToDbPass(PW,salt);//这个是后端使用
        Consumer consumer=new Consumer();
        consumer.setSalt(salt).setUser("user").setPassword(md5PW);
        consumerService.save(consumer);
        return result+"----------------"+PW+"--------------+"+md5PW;
    }
    @RequestMapping("/test1/{passwd}")
    public  String  test1(@PathVariable(value = "passwd")String passwd){
        Consumer consumer=consumerService.findByUser("user");
        String result= md5Util.inputPassToDbPass(passwd,consumer.getSalt());
        String PW= md5Util.inputPassToFormPass(passwd);//这个是前端使用
        String md5PW=md5Util.FormPassToDbPass(PW,consumer.getSalt());//这个是后端使用
        return result+"----------------"+PW+"--------------+"+md5PW;
    }
}
